<?php

class Sectoral_statistics_model extends CI_Model {

    function  __construct() {
        parent::__construct();
        $this->tableName = "mis_sectoral_statistics";
        $this->load->model("functions_model");
    }

    function newSectoral_statistic($added_by) {
        $data = $_POST;
        $data['added_by'] = $added_by;
        $this->db->insert($this->tableName, $data);
    }

    function delete($tid) {
        $data['tid'] = $tid;
        $this->db->where($data);
        $this->db->delete($this->tableName);
    }

    function getOne($tid) {
        $data['tid'] = $tid;
        $this->db->select("tid,org_name,category,position,info_date,source,contact,date_added,added_by");
        $this->db->where($data);
        $this->db->from($this->tableName);
        $this->db->order_by("position", "asc");
        $result = array();
        $query = $this->db->get();
        foreach ($query->result_array() as $row) {
            $row['date_added'] = $this->functions_model->prettyDateTime($row['date_added']);
            $list = $this->consolidate_model->getOrgTypes($row['category']);
            if (count($list) > 0) {
                $row['category'] = $list[0]->name;
            }
            $result[] = $row;
        }
        return $result;
    }

//    function getAll($username) {
//       // $data['added_by'] = $username;
//        $this->db->select("tid,org_name, category,position");
//      //  $this->db->where($data);
//        $this->db->from($this->tableName);
//        $this->db->order_by("position", "asc");
//        $result = array();
//        $query = $this->db->get();
//        foreach ($query->result_array() as $row) {
//            $list = $this->consolidate_model->getOrgTypes($row['category']);
//            if (count($list) > 0) {
//                $row['category'] = $list[0]->name;
//            }
//             $row['more'] = "<a href='javascript:loadUrl(\"#ss_result\",\"" . base_url() . "index.php?/more_info/sectoralStatisticsMoreInfo/" . $row['tid'] . "\")'>More details</a>";
//             unset ($row['tid']);
//            $result[] = $row;
//        }
//        return $result;
//    }

    function adminGetAll($field=NULL, $search=NULL) {
         if ($field and $search) {
            if ($field == 'all') {
                $this->db->like("org_name", $search);
                $this->db->or_like("position", $search);
                $this->db->or_like("source", $search);
                $this->db->or_like("contact", $search);
                $this->db->or_like("added_by", $search);
            } else {
                $data[$field] = $search;
                $this->db->like($data);
            }
        }
        $this->db->select("tid,date_added, org_name, category,position");
        $this->db->from($this->tableName);
        $this->db->order_by("position", "asc");
        $result = array();
        $query = $this->db->get();
        foreach ($query->result_array() as $row) {
            $tid = $row['tid'];
            $row['date_added'] = $this->functions_model->prettyDate($row['date_added']);
            $list = $this->consolidate_model->getOrgTypes($row['category']);
            if (count($list) > 0) {
                $row['category'] = $list[0]->name;
            }
            $row['more'] = "<a href='javascript:loadUrl(\"#ss_result\",\"" . base_url() . "index.php?/more_info/sectoralStatisticsMoreInfo/" . $row['tid'] . "\")'>More details</>  ";

            if ($this->session->userdata("admin") != FALSE) {
                $row['more'].="  <a href='javascript:gotoUrl(\"" . base_url() . "index.php?/admin_mis/deleteSectoral_statistic/$tid\",\"Are you sure to delete this record?\")'>Delete</a> ";
            }
            unset($row['tid']);
            $result[] = $row;
        }
        return $result;
    }

}

?>
